package com.sky.service.user.impl;

import cn.hutool.core.date.DateException;
import com.alibaba.fastjson.JSON;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.mapper.user.MerchantMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.pojo.Merchant;
import com.sky.pojo.User;
import com.sky.properties.JwtProperties;
import com.sky.properties.WeChatProperties;
import com.sky.service.user.UserService;
import com.sky.utils.HttpClientUtil;
import com.sky.utils.JwtUtil;
import com.sky.utils.MapUtils;
import com.sky.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private WeChatProperties weChatProperties;
    @Autowired
    private MerchantMapper merchantMapper;
    @Override
    public UserLoginVO login(UserLoginDTO dto) {
        if (null ==dto || null==dto.getCode()){
            throw new DateException(MessageConstant.ILLEGAL);
        }
       String openId = getOpenId(dto);

      User user =  userMapper.selectById(openId);
      if (null==user){
          user = new User();
          user.setOpenid(openId);
          user.setCreateTime(LocalDateTime.now());
          userMapper.save(user);
      }

        HashMap<String, Object> map = new HashMap<>();
      map.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), map);
         Merchant merchant = merchantMapper.selectById(1L);
        String merchantLocation = MapUtils.addressToLnglat(merchant.getShopAddress());
        BigDecimal delivoryCost = MapUtils.getDelivoryCost(merchantLocation, dto.getLocation());
        UserLoginVO vo = UserLoginVO.builder()
                .openid(openId)
                .token(token)
                .shopName(merchant.getShopName())
                .shopTelephone(merchant.getShopTelephone())
                .shopId(1l)
                .shopAddress(merchant.getShopAddress())
                .id(user.getId())
                .deliveryFee(delivoryCost)
                .build();
        return vo;
    }

    private String getOpenId(UserLoginDTO dto) {
        log.info("dto的参数为:{}",dto);
        HashMap<String, String> map = new HashMap<>();
        map.put("appid",weChatProperties.getAppid());
        map.put("secret",weChatProperties.getSecret());
        map.put("js_code",dto.getCode());
        map.put("grant_type","authorization_code");
        log.info("Map的参数为:{}",map);
        String jsonStr =  HttpClientUtil.doGet("https://api.weixin.qq.com/sns/jscode2session",map);
        Map res = JSON.parseObject(jsonStr, Map.class);
        log.info("jsonStr和Map的参数为:{},{}",dto,res);
        return res.get("openid").toString();
    }
}
